/**
 * Form Layout by Karla.
 * Functional coding by Karla.
 */

package Forms;
import java.text.NumberFormat;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import Classes.Booking;
import Classes.Customers;
import Classes.Payments;
import Classes.Validate;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import teComponents.teJButton;
import teComponents.teJComboBox;
import teComponents.teJFrame;
import teComponents.teJLabel;
import teComponents.teJLabelWarning;
import teComponents.teJList;
import teComponents.teJPanel;
import teComponents.teJTextField;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class BookingPmtsForm  extends teComponents.teJinternalFrame {
	private teJList lstAll;
	private teJLabel lblAmt;
	private teJTextField txtAmt;
	private teJLabel lblBookID;
	private teJTextField txtOutstanding;
	private teJButton btnSearch;
	private teJButton teJButton3;
	private teJButton btnClear;
	private JTextArea lblSearch;
	private teJPanel teJPanel1;
	private JTextPane jTextPane1;
	private teJLabel lblOutstanding;
	private teJButton teJButton1;
	private teJTextField txtCustId;
	private ButtonGroup buttonGroup1;
	private teJButton btnDelPmt;
	private teJButton btnPmt;
	private teJComboBox cboPmtMthd;
	private teJLabel lblPmtMethod;
	private teJTextField txtBookID;
	private teJTextField txtPmtID;
	private teJLabel lblPmtID;
	private teJPanel teSelected;
	private teJComboBox cboCustomer;
	private teJLabel lblCustID;
	private teJLabel lblAssigned;
	private teJLabel lblAll;
	private teJList lstAssigned;
	private Vector<String> errorMsg = new Vector<String>();
	private String customerId;
	Payments genericpmts = new Payments();
	Booking genericbook = new Booking();
	NumberFormat nf = NumberFormat.getCurrencyInstance();
	
	/*	
	public void setCustomerId(int customer) {
		this.customerId = customer;
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				BookingPmtsForm inst = new BookingPmtsForm();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
*/	
	public BookingPmtsForm(String custId) {
		super();
		initGUI(custId);
		customerId = custId;
	}
	public BookingPmtsForm(teJFrame caller, String custId) {
		super(caller);
		initGUI(custId);
		customerId = custId;
	}

	private void initGUI(String custId) {
		try {
			{
				customerId = custId;
				this.setTitle("Maintain Payments");
			}
			{
				this.setSize(832, 444);
			}
			{
				String lSQLAllCust = "select b.*, p.PKGNAME " +
				 "from ictoosd.bookings b, ictoosd.packages p " +
				 "where b.packageid = p.packageid(+) " +
				 "and customerid =  " + customerId;
				Booking lb = new Booking();
                Vector<String> vBook = lb.loadBookingVector(lSQLAllCust);
                lstAll = new teJList(vBook);
                lstAll.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				getContentPane().add(lstAll);
				lstAll.setBounds(12, 89, 295, 238);
				lstAll.addListSelectionListener(new ListSelectionListener() {
					public void valueChanged(ListSelectionEvent evt) {
                       	if (lstAll.getSelectedIndex() != -1)
                    	{
                            getPmts((Booking)lstAll.getSelectedValue());
                    	}
					}
				});
			}
			{
				ListModel teJList1Model = 
					new DefaultComboBoxModel(
							new String[] { });
				lstAssigned = new teJList();
				getContentPane().add(lstAssigned);
				lstAssigned.setModel(teJList1Model);
				lstAssigned.setBounds(318, 90, 224, 238);
				lstAssigned.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						setPmtDetail((Payments)lstAssigned.getSelectedValue());
						btnDelPmt.setEnabled(true);
						btnPmt.setEnabled(false);
						teJButton1.setEnabled(false);
					}
				});
			}
			{
				lblAll = new teJLabel();
				getContentPane().add(lblAll);
				lblAll.setText("Bookings:");
				lblAll.setBounds(12, 70, 93, 14);
				lblAll.setHorizontalAlignment(SwingConstants.LEFT);
			}
			{
				lblAssigned = new teJLabel();
				getContentPane().add(lblAssigned);
				lblAssigned.setText("Payments:");
				lblAssigned.setBounds(318, 71, 139, 14);
				lblAssigned.setHorizontalAlignment(SwingConstants.LEFT);
			}
			{
				lblCustID = new teJLabel();
				getContentPane().add(lblCustID);
				lblCustID.setText("Customer: ");
				lblCustID.setBounds(12, 34, 73, 14);
			}
			{
				txtCustId = new teJTextField();
				getContentPane().add(txtCustId);
				txtCustId.setBounds(85, 30, 93, 18);
			}
			{
				teSelected = new teJPanel();
				getContentPane().add(teSelected);
				teSelected.setBounds(556, 159, 254, 137);
				teSelected.setLayout(null);
				{
					txtPmtID = new teJTextField();
					teSelected.add(txtPmtID);
					txtPmtID.setBounds(121, 7, 120, 18);
				}
				{
					lblPmtID = new teJLabel();
					teSelected.add(lblPmtID);
					lblPmtID.setBounds(65, 9, 56, 14);
					lblPmtID.setText("Pmt ID: ");
					lblPmtID.setHorizontalAlignment(SwingConstants.LEFT);
				}
				{
					txtBookID = new teJTextField();
					teSelected.add(txtBookID);
					txtBookID.setBounds(121, 31, 120, 18);
				}
				{
					lblBookID = new teJLabel();
					teSelected.add(lblBookID);
					lblBookID.setText("Booking ID: ");
					lblBookID.setBounds(33, 31, 88, 14);
					lblBookID.setHorizontalAlignment(SwingConstants.LEFT);
				}
				{
					txtAmt = new teJTextField();
					teSelected.add(txtAmt);
					txtAmt.setBounds(121, 55, 120, 18);
				}
				{
					lblAmt = new teJLabel();
					teSelected.add(lblAmt);
					lblAmt.setText("Pmt Amount: ");
					lblAmt.setBounds(33, 57, 97, 14);
					lblAmt.setHorizontalAlignment(SwingConstants.LEFT);
				}
				{
					lblPmtMethod = new teJLabel();
					teSelected.add(lblPmtMethod);
					lblPmtMethod.setBounds(32, 83, 97, 14);
					lblPmtMethod.setText("Pmt Method: ");
					lblPmtMethod.setHorizontalAlignment(SwingConstants.LEFT);
				}
				{
					ComboBoxModel cboPmtMthdModel = 
						new DefaultComboBoxModel(
								new String[] { "Select a method", "Cash", "Cheque", "Credit Card" });
					cboPmtMthd = new teJComboBox();
					teSelected.add(cboPmtMthd);
					cboPmtMthd.setModel(cboPmtMthdModel);
					cboPmtMthd.setBounds(121, 74, 120, 28);
				}
				{
					txtOutstanding = new teJTextField();
					teSelected.add(txtOutstanding);
					txtOutstanding.setVisible(false);
					txtOutstanding.setBounds(123, 108, 118, 18);
				}
				{
					lblOutstanding = new teJLabel();
					teSelected.add(lblOutstanding);
					lblOutstanding.setText("Balance Owing: ");
					lblOutstanding.setVisible(false);
					lblOutstanding.setBounds(11, 112, 113, 14);
				}
			}
			{
				btnPmt = new teJButton();
				getContentPane().add(btnPmt);
				btnPmt.setText("Record Pmt");
				btnPmt.setBounds(596, 301, 86, 25);
				btnPmt.setToolTipText("Prepare the detail pane for creating a new payment.  Create the payment ID and get the booking ID");
				btnPmt.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						clearPmtDetail();
						
						if (lstAll.getSelectedIndex() != -1)
						{
							Booking b = (Booking)lstAll.getSelectedValue();
							txtBookID.setText(Integer.toString(b.getBookingId()));
							txtOutstanding.setText(nf.format(genericpmts.getOutstanding(b.getBookingId())));
							txtPmtID.setText(genericpmts.getNextPmtId());
							txtOutstanding.setVisible(true);
							lblOutstanding.setVisible(true);
							teJButton1.setEnabled(true);
							btnDelPmt.setEnabled(false);
							btnPmt.setEnabled(false);
						}
						else
						{
							errorMsg.addElement("Please select a booking");
							setError();
						}
					}
				});
			}
			{
				btnDelPmt = new teJButton();
				getContentPane().add(btnDelPmt);
				btnDelPmt.setText("Delete Pmt");
				btnDelPmt.setBounds(596, 332, 86, 25);
				btnDelPmt.setEnabled(false);
				btnDelPmt.setToolTipText("Delete the payment from the database");
				btnDelPmt.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						Payments p = new Payments();
						p.setPmtID(Integer.parseInt(txtPmtID.getText()));
						p.deletePmt();	
						refreshPmts();
						clearPmtDetail();
						btnDelPmt.setEnabled(false);
						btnPmt.setEnabled(true);
						teJButton1.setEnabled(false);
					}
				});
			}
			{
				teJButton1 = new teJButton();
				getContentPane().add(teJButton1);
				teJButton1.setText("Save Pmt");
				teJButton1.setBounds(688, 301, 86, 25);
				teJButton1.setEnabled(false);
				teJButton1.setToolTipText("Save the payment information to the database");
				teJButton1.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						//validate the data in the detail fields
						boolean validCust = ValidateCustomer();
						if (validCust == true)
						{
							Payments p = new Payments();
							p.setPmtID(Integer.parseInt(txtPmtID.getText()));
							p.setBookingID(Integer.parseInt(txtBookID.getText()));
							p.setPmtAmt(Float.parseFloat(txtAmt.getText()));
							p.setPmtMthd((String)cboPmtMthd.getSelectedItem());
							p.savePmt();
							refreshPmts();
							clearPmtDetail();
							teJButton1.setEnabled(false);
							btnPmt.setEnabled(true);
						}
						else
						{
							ListModel newLstAllModel = 
								new DefaultComboBoxModel(errorMsg);
							lstAssigned.setModel(newLstAllModel);
							//**
							//change colour of text to red #c62E04, rgb 198,42,3
							lstAssigned.setForeground(new java.awt.Color(198,42,3));

							//set the selection colour to the background so 
							//it doesn't look like anything's getting selected
							lstAssigned.setSelectionBackground(lstAll.getBackground());
							//set focusable to false
							lstAssigned.setFocusable(false);						}
					}
				});
			}
			{
				teJPanel1 = new teJPanel();
				getContentPane().add(teJPanel1);
				teJPanel1.setBounds(556, 90, 254, 63);
				teJPanel1.setLayout(null);
				{
					lblSearch = new JTextArea();
					teJPanel1.add(lblSearch);
					lblSearch.setText("Search for bookings\nwith a balance owing");
					lblSearch.setBounds(6, 13, 133, 32);
					lblSearch.setBackground(new java.awt.Color(251,253,242));
					lblSearch.setEditable(false);
				}
				{
					btnSearch = new teJButton();
					teJPanel1.add(btnSearch);
					btnSearch.setText("Search");
					btnSearch.setBounds(169, 6, 78, 19);
					btnSearch.setToolTipText("Search the database for all bookings with an outstanding balance for the current customer");
					btnSearch.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							Vector<String> OSBookings = genericbook.loadOSBookings(Integer.parseInt(txtCustId.getText()));
							ListModel newLstAllModel = 
								new DefaultComboBoxModel(OSBookings);
							lstAll.setModel(newLstAllModel);							
						}
					});
				}
				{
					btnClear = new teJButton();
					teJPanel1.add(btnClear);
					btnClear.setText("Clear");
					btnClear.setBounds(169, 31, 78, 19);
					btnClear.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							refreshPmts();
						}
					});
				}
			}
			{
				teJButton3 = new teJButton();
				getContentPane().add(teJButton3);
				teJButton3.setText("Clear");
				teJButton3.setBounds(688, 332, 86, 25);
				teJButton3.setToolTipText("Clear the payment details pane");
				teJButton3.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						lstAll.clearSelection();
						lstAssigned.removeAll();						
					}
				});
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		txtCustId.setText(customerId);
	}
	
	public void getPmts(Booking book)
	{
		Booking b = book;
		refreshPmts("Select * from ictoosd.payments where bookingid = " + b.getBookingId());
		
	}
	
	private void refreshPmts(String sql)
	{
		//lstAll.clearSelection();
		Vector<String> newPmtsVector = genericpmts.loadPaymentsVector(sql);
		
		ListModel newLstAllModel = 
			new DefaultComboBoxModel(newPmtsVector);
		lstAssigned.setModel(newLstAllModel);
		//**
		//change colour of text to dark, dark brown #2b2003, rgb 43,31,2
		lstAll.setForeground(new java.awt.Color(43,31,2));
				
		//reset the colour of the selection background
		lstAll.setSelectionBackground(new java.awt.Color(2,234,231));
		//set focusable back to true
		lstAll.setFocusable(true);
	}

	private void setPmtDetail(Payments pmts) {
		Payments pmt = pmts;
		txtPmtID.setText(Integer.toString(pmt.getPmtID()));
		txtBookID.setText(Integer.toString(pmt.getBookingID()));
		txtAmt.setText(nf.format(pmt.getPmtAmt()));
		cboPmtMthd.setSelectedItem(pmt.getPmtMthd());
	}
	
	private void clearPmtDetail()
	{
		txtPmtID.setText("");
		txtBookID.setText("");
		txtAmt.setText("");
		cboPmtMthd.setSelectedIndex(0);
		txtOutstanding.setVisible(false);
		lblOutstanding.setVisible(false);
	}

	private boolean ValidateCustomer()
	{
		errorMsg.clear();
		boolean b = true;
		if (txtPmtID.getText() == "" || txtPmtID.getText() == null)
		{
			errorMsg.addElement("Press the Record Pmt to initiate");
			b = false;
		}
		else
		{
			if (Validate.genericDouble(txtAmt.getText()) == false)
			{
				errorMsg.addElement("Amount is invalid");
				b = false;
			}
			
			if (cboPmtMthd.getSelectedItem() != "Cash" && cboPmtMthd.getSelectedItem() != "Cheque" && cboPmtMthd.getSelectedItem() != "Credit Card")
			{
				errorMsg.addElement("You must select a pmt method");
				b = false;
			}
		}
		return b;
	}	
	
	private void refreshPmts()
	{
		Booking lb = (Booking)lstAll.getSelectedValue();
		String sql = "Select * from ictoosd.payments where bookingid = " + lb.getBookingId();
		Vector<String> newPmtsVector = genericpmts.loadPaymentsVector(sql);
		
		ListModel newLstAllModel = 
			new DefaultComboBoxModel(newPmtsVector);
		lstAssigned.setModel(newLstAllModel);
		//**
		//change colour of text to dark, dark brown #2b2003, rgb 43,31,2
		lstAssigned.setForeground(new java.awt.Color(43,31,2));
				
		//reset the colour of the selection background
		lstAssigned.setSelectionBackground(new java.awt.Color(2,234,231));
		//set focusable back to true
		lstAssigned.setFocusable(true);
	}
	
	private void setError()
	{
		ListModel newLstAllModel = 
			new DefaultComboBoxModel(errorMsg);
		lstAssigned.setModel(newLstAllModel);
		//**
		//change colour of text to red #c62E04, rgb 198,42,3
		lstAssigned.setForeground(new java.awt.Color(198,42,3));

		//set the selection colour to the background so 
		//it doesn't look like anything's getting selected
		lstAssigned.setSelectionBackground(lstAll.getBackground());
		//set focusable to false
		lstAssigned.setFocusable(false);						
	}
}
